<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\Item;
use App\Models\Base\ServerList;
use App\Service\RequestService;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

/**
 * 道具管理.
 */
class ItemManageController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.gm_tool'));
            $content->description(trans('game.item_manage'));
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(Item::class, function (Grid $grid) {
            $grid->item_no(trans('game.number'));
            $grid->name(trans('game.item_name'));
            $grid->pile_number(trans('game.pile_number'));
            $grid->price(trans('game.price'));
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('item_no', 'ID');
                $filter->like('name', trans('game.item_name'));
            });
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->tools(function ($tools) {
                $button = '<a href="'.route('itemManage.create').'" class="btn btn-sm btn-success pull-right">
                                <i class="fa fa-save"></i>&nbsp;&nbsp;'.trans('game.sync_data').'
                            </a>';
                $tools->append($button);
            });
        });

        return $grid;
    }

    /**
     * [create 同步道具].
     *
     * @Author   王文凡
     * @DateTime 2018-04-19
     *
     * @version  1.0
     *
     * @return string
     */
    public function create()
    {
        $options = '3;0;0;0;0;0;0';
        $server = ServerList::first();
        $ret = RequestService::sendVip($options, $server->server_id);
        if ($ret) {
            return back()->with(admin_toastr(trans('admin.succeeded')));
        } else {
            return back()->with(admin_toastr(trans('admin.failed'), 'error'));
        }
    }
}
